package com.sugon.quartz.simpletrigger;

import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.DateBuilder.*;

import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

import com.sugon.quartz.HelloJob;

/**
 * @desc:
 * @author: luoy
 * @date: 2021-12-09 16:59
 */
public class SimpleTriggerTest {


    public static void main(String[] args) throws SchedulerException {

        //创建一个job
        JobDetail job = JobBuilder.newJob(SimpleJob.class)
                .withIdentity("job", "group").build();


        JobDetail job2 = JobBuilder.newJob(SimpleJob2.class)
                .withIdentity("job2", "group2").build();

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //注册trigger并启动scheduler
        scheduler.scheduleJob( job,get1());
        scheduler.scheduleJob( job2,get2());
        scheduler.start();

    }

    public  static SimpleTrigger get1(){
       //指定时间开始触发，不重复：
        SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger1", "group1")
                .startAt(new Date())                     // some Date
                 .forJob("job", "group")                 // identify job with name, group strings
                .build();
        return trigger;
    }


    public  static SimpleTrigger get2(){
        //指定时间触发，每隔10秒执行一次，重复10次：
        SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger3", "group1")
                .startAt(new Date())  // if a start time is not given (if this line were omitted), "now" is implied
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(10)
                        .withRepeatCount(10)) // note that 10 repeats will give a total of 11 firings
              .forJob("job2","group2") // identify job with handle to its JobDetail itself

                .build();

        return trigger;
    }


    public  static SimpleTrigger get3(){
        //5分钟以后开始触发，仅执行一次：
      SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger5", "group1")
                .startAt(futureDate(5, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
                .forJob("job","group") // identify job with its JobKey
                .build();
        return trigger;
    }


}
